// views/index/shop/mailservice/second/index.js
var BASE_URL = getApp().globalData.BASE_URL
var area = require('../../../../../../utils/area');
var util = require('../../../../../../utils/util');

import Notify from '../../../../../../miniprogram_npm/@vant/weapp/notify/notify';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'',
    amount: '',
    areaList: area.default,
    showarea: false, //地区遮罩
    showmudi: false, //目的遮罩
    area: '',
    name: '',
    name_pinyin: '',
    phone: '',
    wechat: '',
    address: '',
    dizhilist: '',
    userid: '',
    dizhiid: '',
    mudi: '请选择',
    didian: '1人',
    useremail: '',
    minDate: new Date(1970, 0, 1).getTime(),
    currentDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    du_time: '',
    openjiudupop: false,
    openappointment_time: false,
    nowtime: "",
    columns: '',
    pop_type: '',
    appointment: '是',
    appointment_time: 'yyyy/mm/日',
    tianshu: '',
    zhongxin: ''
  },

  //语言切换
  change_appointment: function (e) {
    this.setData({
      appointment: e.currentTarget.dataset.appointment
    })
  },
  //打开目的pop
  openmudipop: function () {
    this.setData({
      showmudi: true,
      pop_type: 'mudi',
      columns: ['请选择', '北京领区', '上海领区', '广州领区', '成都领区', '沈阳领区']
    })
  },
  //打开地点pop
  opendidianpop: function () {
    this.setData({
      pop_type: 'didian',
      showmudi: true,
      columns: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
    })
  },
  //打开签证中心pop
  openzhongxinpop: function () {
    this.setData({
      pop_type: 'zhongxin',
      showmudi: true,
      columns: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
    })
    if (this.data.mudi == '北京领区') {
      this.setData({
        columns: ['北京签证中心', '济南签证中心', '西安签证中心', '武汉签证中心', '长沙签证中心', ]
      })
    }
    if (this.data.mudi == '上海领区') {
      this.setData({
        columns: ['上海签证中心', '杭州签证中心', '南京签证中心']
      })
    }
    if (this.data.mudi == '广州领区') {
      this.setData({
        columns: ['广州签证中心', '深圳签证中心', '福州签证中心' ]
      })
    }
    if (this.data.mudi == '成都领区') {
      this.setData({
        columns: ['成都签证中心', '重庆签证中心', '昆明签证中心' ]
      })
    }
    if (this.data.mudi == '沈阳领区') {
      this.setData({
        columns: ['沈阳签证中心']
      })
    }
  },
  //打开预约时间pop
  openappointment_time: function () {
    this.setData({
      openappointment_time: true,
    })
  },

  //改变地点或目的
  change_mudi(e) {
    if (this.data.pop_type == 'mudi') {
      this.setData({
        mudi: e.detail.value,
        zhongxin:e.detail.value.substring(0,2)+'签证中心'
      })
    }
    if (this.data.pop_type == 'didian') {
      this.setData({
        didian: e.detail.value + '人',
        amount: e.detail.value * 799
      })
    }
    if (this.data.pop_type == 'zhongxin') {
      this.setData({
        zhongxin: e.detail.value,
      })
    }
    this.setData({
      showmudi: false
    })
  },


  //确定时间
  change_jiudu: function () {
    this.setData({
      openjiudupop: false,
      du_time: this.changeTime(this.data.nowtime)
    })
  },
  change_appointment_time: function () {
    this.setData({
      openappointment_time: false,
      appointment_time: this.changeTime(this.data.nowtime)
    })
  },
  //切换时间
  getColumnValue(event) {
    console.log(event)
    this.setData({
      nowtime: event.detail,
    });
  },
  //关闭时间pop
  onClose: function () {
    this.setData({
      openjiudupop: false,
      showmudi: false,
      openappointment_time: false
    })
  },

  //打开时间pop
  openjiudupop: function () {
    this.setData({
      openjiudupop: true
    })
  },

  // 时间戳转换
  changeTime(time) {
    if (time) {
      var oDate = new Date(time * 1),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oTime = oYear + "/" + this.getBz(oMonth) + "/" + this.getBz(oDay)
      return oTime;
    } else {
      return "";
    }
  },
  // 补0
  getBz(num) {
    if (parseInt(num) < 10) {
      num = "0" + num;
    }
    return num;
  },

























  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      amount: 799,
      type:options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this
    if (util.iflogin()) {
      var userinfo = wx.getStorageSync("USER")
      this.setData({
        userid: userinfo.id,
        email: userinfo.email,
        useremail: userinfo.email
      })
      _this.getdizhilist()
    }
  },
  //获得地址
  getdizhilist: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/dizhi`,
      method: "GET",
      data: {
        id: _this.data.userid
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        _this.setData({
          dizhilist: res.data
        })
      }
    })
  },

  //使用地址
  userdizhi: function (e) {
    console.log(e)
    var dizhi = this.data.dizhilist[e.currentTarget.dataset.index]
    this.setData({
      dizhiid: dizhi.id,
      name: dizhi.sname,
      name_pinyin: dizhi.pingyin,
      phone: dizhi.dianhua,
      wechat: dizhi.wechat,
      area: dizhi.qvyu,
      address: dizhi.dizhi,
      useremail: dizhi.email
    })
  },
  //删除地址
  deldizhi: function (e) {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定删除此地址吗?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: `${BASE_URL}/gongzheng/deletedizhi`,
            method: "GET",
            data: {
              id: e.currentTarget.dataset.id
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {
              _this.getdizhilist()
            }
          })
        } else if (res.cancel) {}
      }
    })
  },

  //填写信息的下一步按钮
  gothird: function () {
    var _this = this
    if (!_this.ifhanzi(_this.data.name)) {
      Notify('姓名输入错误');
    }

    if (!_this.ifwewhat(_this.data.wechat)) {
      Notify('微信号输入错误');
    }
    if (!_this.codeVerification(_this.data.phone)) {
      Notify('请输入正确的手机号');
    }

    if (!_this.ifenglish(_this.data.name_pinyin)) {
      Notify('拼音输入错误');
    }
    if (_this.data.address == "") {
      Notify('详细地址不能为空');
    }
    var userinfo = wx.getStorageSync("USER")

    if (_this.ifhanzi(_this.data.name) &&
      _this.ifwewhat(_this.data.wechat) && _this.codeVerification(_this.data.phone) && _this.ifenglish(_this.data.name_pinyin) && _this.data.address !== ""
    ) {
      wx.request({
        url: `${BASE_URL}/visa/visasave`,
        method: "POST",
        data: {
          sname: _this.data.name,
          pingyin: _this.data.name_pinyin,
          dianhua: _this.data.phone,
          wechat: _this.data.wechat,
          qvyu: _this.data.area,
          dizhi: _this.data.address,
          email: _this.data.useremail,
          uid: _this.data.userid,
          beizhu: _this.data.beizhu,
          diqian: _this.data.mudi,
          diqian_center: _this.data.zhongxin,
          num_trips: parseInt(_this.data.didian),
          travel_date: _this.data.du_time,
          stay_days: parseInt(_this.data.tianshu),
          dianhua: _this.data.phone,
          type: 'xiaochengxv',
          uid: userinfo.id,
          cate: _this.data.type,
          id: _this.data.dizhiid,
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          wx.navigateTo({
            url: `../sureorder/index?id=${res.data.id}&type=${_this.data.type}`
          })
        }
      })

    }
  },














  //手机号正则
  codeVerification: function (phone) {
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    return phoneCodeVerification.test(phone);
  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
  //汉字正则
  ifhanzi: function (pinyin) {
    let ifhanzi = /^[\u4e00-\u9fa5]+$/;
    return ifhanzi.test(pinyin);
  },
  //微信号正则
  ifwewhat: function (pinyin) {
    let ifwewhat = /^[0-9a-zA-Z]*$/g;
    return ifwewhat.test(pinyin);
  },
  //邮箱验证
  ifemail: function (email) {
    let ifemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return ifemail.test(email);
  },



  //打开地区选择遮罩层
  open_area: function (e) {
    this.setData({
      showarea: true
    })
  },
  //选择好地区并关闭地区选择器遮罩呈
  changeuserarea: function (e) {
    console.log(e)
    var str = []
    e.detail.values.forEach((item, index) => {
      if (item) {
        str.push(item.name)
      }
    })
    str = str.join('--')
    this.setData({
      showarea: false,
      area: str
    })
  },
  //关闭遮罩
  closeuserarea: function () {
    this.setData({
      showarea: false
    })
  },

})